﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using RoketSolutions.SP2010.ImageRotatorWebPart.Constants;
using RoketSolutions.SP2010.ImageRotatorWebPart.WebControls;
using RoketSolutions.SP2010.ImageRotatorWebPart.WebParts.ToolParts;
using RoketSolutions.SP2010.Infrostructure.UI.Controls.Interfaces;
using RoketSolutions.SP2010.Infrostructure.UI.WebParts;

namespace RoketSolutions.SP2010.ImageRotatorWebPart.WebParts.ImageRotator
{
    [ToolboxItemAttribute(false)]
    public class ImageRotator : LocalizableWebPart
    {
        const string ControlUrl = @"~/_controltemplates/RoketSolutions.SP2010.ImageRotatorWebPart/ImageRotatorControl.ascx";

        #region / Properties /

        private ImageRotatorSettings _settings;

        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public ImageRotatorSettings Settings
        {
            get
            {
                if (_settings == null)
                {
                    _settings = new ImageRotatorSettings
                                    {
                                        Effect = "scrollLeft",
                                        PagerType = "Bubbles",
                                        Speed = "Normal",
                                        PauseOnHover = true,
                                        ShowPrevNextButton = false,
                                        Timeout = 3000
                                    };
                }
                return _settings;
            }
            set
            {
                _settings = value;
                if (ImageRotatorControl != null)
                {
                    ImageRotatorControl.Settings = _settings;
                }
            }
        }

        protected Control TargetControl { get; set; }

        protected override string ResourcesFolderPath
        {
            get
            {
                // return path of the folder where all localization resources are located
                return ResourcesLocation.GetFolderPath();
            }
        }

        private IImageRotatorControl ImageRotatorControl
        {
            get { return TargetControl as IImageRotatorControl; }
        }

        #endregion

        #region / Methods /

        protected override void OnInit(EventArgs e)
        {
            EnsureChildControls();
            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {
            if (TargetControl == null)
            {
                // try to load control
                TargetControl = Page.LoadControl(ControlUrl);
                if (TargetControl != null)
                {
                    // check whether the control supports localization
                    if (TargetControl is ILocalizableControl)
                    {
                        // set localization settings
                        ((ILocalizableControl) TargetControl).SetupConfig(ResourceConfig);
                    }

                    if (ImageRotatorControl != null)
                    {
                        // apply settings
                        ImageRotatorControl.Settings = Settings;
                    }

                    Controls.Add(TargetControl);
                }
                base.CreateChildControls();
            }
        }

        public override ToolPart[] GetToolParts()
        {
            var tools = new List<ToolPart>();

            tools.Add(new ImageRotatorToolPart(Settings));
            tools.AddRange(base.GetToolParts());

            return tools.ToArray();
        }

        #endregion

    }
}
